/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package exampleTwo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Patrik
 */
public class Main
{
    public static void main(String[] args)
    {
        if (JOptionPane.showConfirmDialog(null, "Do you want to use the advanced mode?", "Question", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
	{
	    new Advanced();
	}
	else
	{
	    try
	    {
		Example.main(null);
	    }
	    catch (IOException ex)
	    {
		JOptionPane.showMessageDialog(null, "The program crashed sucessfully!\n\n" + ex, "Program Crash", JOptionPane.ERROR_MESSAGE);
		System.exit(14);
	    }
	}
    }
}

class Example
{
    public static void main(String[] arg) throws IOException{
	String filnamn1 = JOptionPane.showInputDialog("Vad heter filen som ska läsas?");

	// Läs in filen utan någon som helst kontroll om den finns.
	// Det är omöjligt att stava fel när man skriver inputs till java program
	BufferedReader inström = new BufferedReader(new FileReader(filnamn1));

	String filnamn2 = JOptionPane.showInputDialog("Vad hter filen som ska skrivas?");

	// ÅÄÖ är bra att ha i variabelnamn, speciellt om man ska ge koden till en Amerikan
	PrintWriter utström = new PrintWriter(new BufferedWriter(new FileWriter(filnamn2)));

	int radNr = 0;
	while (true) {
	    String rad = inström.readLine();

	    if (rad == null)
		break;

	    radNr++;
	    utström.println(radNr + ": " + rad);
	}

	System.out.println(radNr + " rader kopierade från " + filnamn1 + " till " + filnamn2);

	// Varför inte göra detta innan man skriver ut att man är klar?
	utström.close();

	// Se till att programmet verkligen stängs, vi kan inte lita på att man inte försöker göra något annat
	System.exit(0);
    }
}

class Advanced
{
    public Advanced()
    {
	JFileChooser chooser = new JFileChooser();

	chooser.setDialogType(JFileChooser.OPEN_DIALOG);
	chooser.setDialogTitle("Select the file to copy");

	if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
	{
	    System.exit(0);
	}

	File read = chooser.getSelectedFile();

	if (!read.exists())
	{
	    JOptionPane.showMessageDialog(null, "Coult not find " + read.getName() + "!", "Error", JOptionPane.ERROR_MESSAGE);
	    System.exit(11);
	}
	if (!read.canRead())
	{
	    JOptionPane.showMessageDialog(null, "Could not read from " + read.getName() + "!", "Error", JOptionPane.ERROR_MESSAGE);
	    System.exit(12);
	}

	// Re-use chooser
	chooser.setDialogType(JFileChooser.SAVE_DIALOG);
	chooser.setDialogTitle("Select the file to copy to");

	if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
	{
	    System.exit(0);
	}

	File write = chooser.getSelectedFile();

	if (write.exists())
	{
	    if (write.canWrite())
	    {
		JOptionPane.showMessageDialog(null, "The file exists, but is not writeable", "Error", JOptionPane.ERROR_MESSAGE);
		System.exit(12);
	    }
	    else if (JOptionPane.showConfirmDialog(null, "The file exists, do you want to overwrite?", "Question", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION)
	    {
		System.exit(0);
	    }

	}
	else
	{
	    try
	    {
		write.createNewFile();
	    }
	    catch (IOException ex)
	    {
		ex.printStackTrace();
		JOptionPane.showMessageDialog(null, "The file " + write.getName() + " is not writeable!", "Error", JOptionPane.ERROR_MESSAGE);
		System.exit(13);
	    }
	}

	chooser = null; // Get rid of this

	try
	{
	    BufferedReader in  = new BufferedReader(new FileReader(read));
	    BufferedWriter out = new BufferedWriter(new FileWriter(write));
	    int data;
	    while ((data = in.read()) != -1)
	    {
		System.out.print((char) data);
		out.write(data);
	    }
	    out.close();
	    JOptionPane.showMessageDialog(null, "The file copied successfully!", "Information", JOptionPane.INFORMATION_MESSAGE);
	}
	catch (Exception ex)
	{
	    JOptionPane.showMessageDialog(null, "Unknown error occured:\n\n" + ex, "Error", JOptionPane.ERROR_MESSAGE);
	    ex.printStackTrace();
	    System.exit(13);
	}
    }
}